package com.fxc.handler;

import com.fxc.MySQLRemoteService;
import com.fxc.crowd.constant.CrowdConstant;
import com.fxc.crowd.util.ResultEntity;
import com.fxc.entity.VO.AddressVO;
import com.fxc.entity.VO.MemberLoginVO;
import com.fxc.entity.VO.OrderProjectVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author FXC
 * @version 1.0
 * @date 2021/12/17 19:33
 */
@Controller
public class OrderHandler {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;


    private Logger logger = LoggerFactory.getLogger(OrderHandler.class);

    @RequestMapping("/save/address")
    public String saveAddress(AddressVO addressVO, HttpSession session){

        // 执行地址信息域的保存
        ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressRemote(addressVO);

        logger.debug("地址保存处理结果" + resultEntity.getResult());

        // 从session域中取得orderProjectVO对象
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT);

        // 从对象中获取returnCount
        Integer returnCount = orderProjectVO.getReturnCount();

        // 重定向到指定地址，重新进入确认订单的页面
        return "redirect:http://localhost:8081/order/confirm/order/" + returnCount;
    }

    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") Integer returnCount, HttpSession session){

        // 1.把接收到的回报数量合并到Session域
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT);

        orderProjectVO.setReturnCount(returnCount);

        session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT, orderProjectVO);

        // 2.获取当前用户的id
        MemberLoginVO memberLoginVO = (MemberLoginVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);

        Integer memberId = memberLoginVO.getId();

        // 3.查看目前的收货地址数据
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            List<AddressVO> data = resultEntity.getData();

            session.setAttribute("addressVOList", data);
        }

        return "confirm_order";
    }

    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(
            @PathVariable("projectId") Integer projectId,
            @PathVariable("returnId") Integer returnId,
            HttpSession session) {

        ResultEntity<OrderProjectVO> resultEntity = mySQLRemoteService.getOrderProjectVORemote(projectId, returnId);

        if (ResultEntity.SUCCESS.equals(resultEntity.getResult())){

            OrderProjectVO data = resultEntity.getData();

            // 为了后续能保持数据把orderProjectVO数据，存入session域
            session.setAttribute(CrowdConstant.ATTR_NAME_ORDER_PROJECT, data);
        }

        return "confirm_return";
    }
}
